//
//  CAReplicatorLayerViewController.m
//  CoreAnimationDemo
//
//  Created by 王亮 on 2017/8/10.
//  Copyright © 2017年 王亮. All rights reserved.
//

#import "CAReplicatorLayerViewController.h"

///给视图生成反射视图，即倒影
@interface ReflectionView : UIView

@end

@implementation ReflectionView

- (id)init {
    self = [super init];
    if (self) {
        //
        [self setUp];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //
        [self setUp];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setUp];
}

+ (Class)layerClass {
    return [CAReplicatorLayer class];
}

- (void)setUp {
    CAReplicatorLayer *layer = (CAReplicatorLayer *)self.layer;
    layer.instanceCount = 2;
    
    //move reflection instance below origin and flip vertically
    CGFloat verticallyHeight = self.bounds.size.height + 2;
    CATransform3D transform = CATransform3DIdentity;
    transform = CATransform3DTranslate(transform, 0, verticallyHeight, 0);
    transform = CATransform3DScale(transform, 1, -1, 0);
    layer.instanceTransform = transform;
    
    layer.instanceAlphaOffset = -0.5;
}

@end


@interface CAReplicatorLayerViewController ()
@property (nonatomic, strong)UIView *containerView;

@end

@implementation CAReplicatorLayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
    self.containerView.backgroundColor = [UIColor grayColor];
    self.containerView.center = self.view.center;
    self.containerView.userInteractionEnabled = YES;
    [self.view addSubview:self.containerView];
    
    CGFloat line = 0;
    NSInteger num = 10;
    CGFloat topRadius = 360/num/2.0;
    CGFloat leftRadius = (90-topRadius);
    CGFloat bottomLine = line;
    CGFloat height = 0;
    height = self.containerView.bounds.size.height/4.0;
    bottomLine = height/(tan(leftRadius*M_PI/180))*2;

    //create a replicator layer and add it to our view
    CAReplicatorLayer *replicator = [CAReplicatorLayer layer];
    replicator.frame = self.containerView.bounds;
    [self.containerView.layer addSublayer: replicator];
    
    //config the replicator
    replicator.instanceCount = 10;
    
    //add transform for each instance
    CATransform3D transform = CATransform3DIdentity;
//    transform = CATransform3DTranslate(transform, 0, bottomLine/10.0, 0);
    transform = CATransform3DRotate(transform, M_PI/5, 0, 0, 1);
//    transform = CATransform3DTranslate(transform, 0, -bottomLine/10.0, 0);
    transform = CATransform3DScale(transform, 0, 0, 0);
    replicator.instanceTransform = transform;
    
    //create sublayer
    CALayer *sublayer = [CALayer layer];
    sublayer.frame = CGRectMake(self.containerView.bounds.size.width/2.0-bottomLine/2.0, 0, bottomLine, bottomLine);
    sublayer.position = CGPointMake(sublayer.position.x+bottomLine/2.0, sublayer.position.y+bottomLine/2.0);
    sublayer.anchorPoint = CGPointMake(1, 1);
//    sublayer.position = CGPointMake(self.containerView.bounds.size.width/2.0, self.containerView.bounds.size.height/2.0);
    sublayer.backgroundColor = [UIColor whiteColor].CGColor;
    [replicator addSublayer:sublayer];
    
    //add reflectionview
    ReflectionView *reflectionView = [[ReflectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.containerView.frame), 100, 50)];
    reflectionView.center = CGPointMake(self.view.bounds.size.width/2.0, reflectionView.center.y);
    [self.view addSubview:reflectionView];
    UILabel *label = [[UILabel alloc] init];
    label.frame = reflectionView.bounds;
    label.text = @"12345";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:30.0];
    [reflectionView addSubview:label];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

